package com.watersys.httpserver;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream.PutField;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;


import android.os.AsyncTask;



public class UploadAsyncTask extends AsyncTask<Object, Integer, String>{
	
	public ProgressDialog pd;
	public long totalSize;
	public Context context;
	public File file;//上传的文件
	public String url="";
	
	public Map<String, String> param;//上传的参数

	public UploadAsyncTask(Context context,File file,Map<String, String> param){
		this.context=context;
		this.file=file;
		this.param=param;
	}

	//此方法是异步，不在UI线程
	@Override
	protected String doInBackground(Object... params) {
		HttpPost post = new HttpPost(url);
        HttpClient httpclient=new DefaultHttpClient();
		MultipartEntity entity = new MultipartEntity();
	
		//取参数
		if (param != null && !param.isEmpty()) {
			for (Map.Entry<String, String> entry : param.entrySet()) {
				try {
					entity.addPart(entry.getKey(), new StringBody(entry.getValue()));
					
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		//取上传文件
		if (file != null && file.exists()) {
			entity.addPart("image", new FileBody(file));			
		}
		post.setEntity(entity);
		try {
			HttpResponse response = httpclient.execute(post);
			//返回状态
			int stateCode = response.getStatusLine().getStatusCode();
			StringBuffer sb = new StringBuffer();
			if (stateCode == HttpStatus.SC_OK) {
				HttpEntity result = response.getEntity();
				if (result != null) {
					InputStream is = result.getContent();
					BufferedReader br = new BufferedReader(
							new InputStreamReader(is));
					String tempLine;
					while ((tempLine = br.readLine()) != null) {
						sb.append(tempLine);
					}
				}
			}
			post.abort();
			return sb.toString();
		} catch (ClientProtocolException e) {
			
			e.printStackTrace();
			return "error";
		} catch (IOException e) {
			return "error";
			
		}
            
		
	}


   //在doInBackground执行完后热行应该方法，在UI线程上热行
	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
	}

	//该方法运行在UI线程中
	@Override
	protected void onPreExecute() {
		pd = new ProgressDialog(context);
		pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		pd.setTitle("上传中.......");
		pd.setCancelable(false);
		
		pd.setButton("取消上传", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				System.out.println(123);
				
			}
		});
	
		pd.show();
	}

	//当调用 publishProgres方法是回触发该方法
	@Override
	protected void onProgressUpdate(Integer... values) {
		// TODO Auto-generated method stub
		super.onProgressUpdate(values);
	}

}
